﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace NetFtp.Core.Ftp
{
    /// <summary>
    /// Gère les dossiers temporaires
    /// </summary>
    public class FtpSessionFolder
    {
        private string _id;
        private string _folder;

        const int nbminutes = 240;

        /// <summary>
        /// Constructeur : crée le dossier temporaire si nécessaire
        /// </summary>
        /// <param name="chemin">Chemin</param>
        public FtpSessionFolder(string chemin, string id)
        {
            try
            {
                //creation du dossier
                if (id == "")
                {
                    Guid idtmp = Guid.NewGuid();
                    _id = idtmp.ToString();
                }
                else
                {
                    _id = id;
                }
                _folder = Path.Combine(chemin, _id);


                if (!Directory.Exists(_folder))
                {

                    DirectoryInfo t = Directory.CreateDirectory(_folder);

                }

                //verification des dossiers plus utilises
                foreach (string dossier in Directory.GetDirectories(chemin))
                {
                    DirectoryInfo infodossier = new DirectoryInfo(dossier);
                    if (infodossier.CreationTime.AddMinutes(nbminutes) < DateTime.Now)
                        infodossier.Delete(true);
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// Chemin du dossier
        /// </summary>
        public string Folder
        {
            get { return _folder; }
        }

        /// <summary>
        /// Id de la session
        /// </summary>
        public string Id
        {
            get { return _id; }
        }
    }
}